import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [

    {
      path: '/register',
      name: 'register',
      component: () => import('../views/register/index.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue'),
    },
    {
      path: '/email',
      name: 'email',
      component: () => import('../views/login/email.vue'),
    },
    {
      path: '/phone',
      name: 'phone',
      component: () => import('../views/login/phone.vue'),
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/home/index.vue'),
    },
  ],
})
const list = ['/404', '/register', '/login','/home']
router.beforeEach((to, from, next) => {
  let accessToken = localStorage.getItem('accessToken')
  if (!accessToken) {
    if (list.includes(to.path)) {
      next()

    }
    else {
      next('/login')

    }

  }
  else {
    next()

  }


})
export default router
